<?php

namespace App\Http\Controllers;

use App\Constant\Response;
use App\Jobs\testJob;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Storage;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function runJob()
    {
        $data = [
            ['name' => '张飞','email'  => rand(1,888).'zhangfeis@qq.com','password' => md5('张飞'.time())],
            ['name' => '张飞1','email' => rand(1,888).'zhangfeis@qq.com','password' => md5('张飞').time()],
            ['name' => '张飞2','email' => rand(1,888).'zhangfeis@qq.com','password' => md5('张飞').time()],
            ['name' => '张飞3','email' => rand(1,888).'zhangfeis@qq.com','password' => md5('张飞').time()],
            ['name' => '张飞4','email' => rand(1,888).'zhangfeis@qq.com','password' => md5('张飞').time()],
            ['name' => '张飞5','email' => rand(1,888).'zhangfeis@qq.com','password' => md5('张飞').time()],
        ];
        foreach ($data as $V){
            TestJob::dispatch($V);
        }
    }

    public function upload(Request $request)
    {
        $file = $request->file('image');
        //判断是否为空
        if(empty($file)){
            return back()->withErrors('请上传logo')->withInput();
        }
        //取得上传文件的大小
        $sizes = $file->getSize();
        $size = $sizes/1024;

        //格式不对提醒
        $ext = $file->getClientOriginalExtension();     // 扩展名

        // 获取文件相关信息
        $realPath = $file->getRealPath();   //临时文件的绝对路径
        //上传路径
        $fileName =env('FILEPATH').time().'.'.$ext;  //拼接图片名
        return MediaService::uploadImage($fileName,$realPath);
    }

    public static function success($data = null)
    {
        $arr = [];
        $arr['success'] = true;
        $arr['err_code'] = -1;
        $arr['err_msg'] = '';
        $arr['data'] = $data;

        return response()->json($arr);
    }


    public static function error($msg = '', $redirect = '', $debugMsg = null)
    {
        $res = array('err_msg' => $msg, 'success' => false, 'data'=> null);
        if ($redirect != '')
            $res['redirect'] = $redirect;

        return response()->json($res);
    }
}
